package com.example.treinamentotiroaoalvo;

import com.example.treinamentotiroaoalvo.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class GameLoop extends View implements Runnable
{
	public static Resources res;
	public static int canvasWidth;
	public static int canvasHeight;
	public int score = 0;
	public int tries = 5;
	
	private long time = 1;
	public long timer = 0;
	public long clock = 0;
	public long startPoint = 0;
	public long timerBetRespawn = 2000;
	
	Paint paint;
	Context context;
	
	public boolean shot = false;
	
	public TargetBehaviour target;
	public BackgroundBehaviour background;
	public ButtonBehaviour_Shoot button;
	public AimBehaviour aim;

	public GameLoop(Context context)
	{
		super(context);
		
		this.context = context;
		SoundManager.getInstance().setup(context);
		
		Log.i("TiroAoAlvo","Create Gameloop");
		this.res = this.getResources();
		
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTextSize(15); 

		Thread thread = new Thread(this);
		thread.start();
	}

	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		super.onSizeChanged(w, h, oldw, oldh);
		GameLoop.canvasWidth = getWidth();
		GameLoop.canvasHeight = getHeight();
		

			target = new TargetBehaviour();
			Log.i("TiroAoAlvo", "Instanciou");
		
		background = new BackgroundBehaviour();
		button = new ButtonBehaviour_Shoot();
		aim = new AimBehaviour();
	}

	public void draw(Canvas canvas)
	{
		background.draw(canvas);
		
		if (target != null)
			target.draw(canvas);

		button.draw(canvas);
		aim.draw(canvas);

		canvas.drawText("Pontos: " + score, 10, 30, paint);
		canvas.drawText("Erros: " + tries, 10, 50, paint);
	}

	public void run()
	{
		startPoint = System.currentTimeMillis();
		while (true)
		{
			try
			{
				Thread.sleep(time);
			} catch (InterruptedException e)
			{
				Log.e(MainActivity.TAG, "interrupcao do run()");
			}

			update();
			postInvalidate();
		}
	}

	private void update()
	{
		if(clock < 1000)
		{
			clock++;
			
			if(clock == 1000)
			{
				timer += 1;
				clock = 0;
			}
		}

		if(target != null)
		{
			target.Update();
		
			if(target.posY > canvasHeight || target.posX > canvasWidth || target.posX < 0 - target.target.getWidth() || target.posY < 0 - target.target.getHeight())
			{
				NewTarget();
				tries--;
			}
			
			if(tries == 0)
			{
				Reset();
			}
		}
	}
	
	public boolean onTouchEvent(MotionEvent event)
	{	
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			int x = (int) event.getX();
			int y = (int) event.getY();
			
			if(button.Detect(x, y))
			{
				if(shot == false)
				{
					shot = true;
					if(target.getTargetRect().contains(aim.getAimRect()))
					{
						NewTarget();
						score += 5;
					}
					else if(target.getTargetRect().contains(aim.getAimRect().centerX(),aim.getAimRect().centerY()))
					{
						NewTarget();
						score++;
					}
					
					SoundManager.getInstance().playSound(R.raw.shootsound, "shootSound", 1, context);
				}
			}
		}
		return super.onTouchEvent(event);
	}
	
	public void NewTarget()
	{		
		target = new TargetBehaviour();
		shot = false;
	}
	
	public void Reset()
	{
		score = 0;
		tries = 5;
	}
}